<?php
require_once APP  . '/application/admincontrollers/AdminAction.php';
/**
 * PrivilegeAction 用户权限管理控制器
 * 
 * @author RD / anthony
 * @version 
 */
    
class PrivilegeController extends AdminAction
{
    
    const KEYWORD = 'abcdefghijkmnpqrstuvwxyzABCDEFGHIJKMNPQRSTUVWXYZ23456789~!@#$%^&*()_';
    /**
     * initial function
     *
     */
    public function init()
    {
        require_once APP.'/application/models/AdminUser.php';
        //后台管理员对象
        $this->_admin = new AdminUser($this->db);
    }
    
    
    /**
     * 后台管理员用户管理
     */
    public function adminmanageAction ()
    {
        $nowp = $this->getParam ('nowp', 1);
        $per_page = 15;
        
        $arr_logininfo = AdminUser::getLoginInfo();
        if ($arr_logininfo['gid'] == 0){
        	$where = '';
        }elseif ($arr_logininfo['gid'] == 1){
        	$where = ' where gid=1';
        }elseif ($arr_logininfo['gid'] == 2){
        	$where = ' where gid=2 or gid=3 or gid=4';
        }elseif ($arr_logininfo['gid'] == 3 || $arr_logininfo['gid'] == 4){
        	$where = ' where id=' . $arr_logininfo['id'];
        }

        $result = $this->_admin->listAdminUser ($where, $nowp, $per_page);

        $this->view->list = $result['list'];
        $this->view->page = $result['page'];

    }


    /**
     * 后台管理员用户删除
     */
    public function admindelAction ()
    {
        $id = $this->getParam ('id');
        if ($this->_admin->getUserCount() == 1)
        	$this->view->msg = '最后一个账号不能被删除';
        else{
	        $affected_rows = $this->_admin->delAdminUser ($id);
	        
	        if ($affected_rows) {
	            $this->view->msg = '删除成功';
	        } else {
	            $this->view->msg = '删除失败';
	        }
        }
        $this->view->url     = $this->url($this->view->fullUrl, 'privilege', 'adminmanage');

        $this->showmessage ();
    }


    /**
     * 后台管理员用户添加
     */
    public function adminaddAction ()
    {
        $username  = $this->getParam ('username');
        $password  = $this->getParam ('password');
        $gid = $this->getParam("gid");
        //添加用户
        if (
        !empty ($username) &&
        !empty ($password)) {
            try{
                $this->_admin->addAdminUser ($this->getAllParams ());
                $this->view->msg = '添加成功';
                $this->view->url = $this->url($this->view->fullUrl, 'privilege', 'adminmanage');
            } catch (Exception $e) {
                $this->view->msg = '添加失败, 可能用户名已存在';// . $e->getMessage ();
                $this->view->url = $this->url($this->view->fullUrl, 'privilege', 'adminadd');
            }
            $this->showmessage();
        }
        
        //系统随机密码
        $p                             = str_shuffle (self::KEYWORD);
        $pos                           = mt_rand (0, strlen ($p) - 20);
        $this->view->password          = substr ($p, $pos, mt_rand (15, 20));
    }


    /**
     * 修改后台用户的密码
     */
    public function adminpasswordAction ()
    {

        $id = $this->getParam ('id');

        $password = $this->getParam ('password');

        if (!empty ($password) && !empty ($id)) {
            $this->_admin->changeAdminUserPassword ($id, trim($password));

            $LoginInfo = $this->_admin->getLoginInfo ();
            //若是修改当前用户，则更新登陆信息
            if ($LoginInfo['id'] == $id) {
                $this->_admin->login ($LoginInfo['username'], trim($password));
            }

            $this->view->url = $this->url($this->view->fullUrl, 'privilege', 'adminmanage');
            $this->view->msg = '修改成功';
            $this->showmessage();
        }

        $this->view->content = $this->_admin->showAdminUser ($id);
    }
    
    
    
    public function flushAction ()
    {
        $this->setAutoRender (false);
        echo '<a href="' . $this->url ($this->view->baseUrl, 'privilege','doflush') . '">清空</a>';
    }
}
